﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.LongER.Framework.Utils;
using Com.LongER.UI;
using Com.LongER.Business.ZAH002;
using Com.LongER.Framework.Utils.LogUtils;

public partial class ZAH002 : ABasePage
{
    private Log log = Log.GetInstance();

    protected void Page_Load(object sender, EventArgs e)
    {
        Label lblPageId = (Label)Master.FindControl("lblPageId");
        lblPageId.Text = "ZAH002";

        Label lblPageName = (Label)Master.FindControl("lblPageName");
        lblPageName.Text = "密码变更画面";

        try
        {
            if (Session["LoginInfo"] != null)
            {
                LoginInfo LoginInfo = (LoginInfo)Session["LoginInfo"];
                //
                if ( ! LoginInfo.USERID.Equals(LoginInfo.NBSP))
                {
                    this.txt用户ID.Text = LoginInfo.USERID;
                    this.GetUser(LoginInfo.USERID);

                }
                
            }
        }
        catch (HttpException ex)
        {
            log.Error(ex.ToString());
        }
        
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn执行_Click(object sender, EventArgs e)
    {

        this.RemoveCssClass(this.txt用户ID, CSS_TEXT_ERROR);
        this.RemoveCssClass(this.txt旧密码, CSS_TEXT_ERROR);

        //
        if(this.txt用户ID.Text.Equals(String.Empty)){

            this.SetMessage("ZAH002", "EZAH002001", MSG_LEVEL.Error);

            this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
            this.txt用户ID.Focus();
            return;
        }

        if (this.txt用户ID.Enabled == true)
        {
            //
            Boolean isExists = false;
            isExists = this.GetUser(this.txt用户ID.Text);

            if (!isExists) return;

        }
        else
        {


            Boolean isChanged = false;
            CZAH002 instance = new CZAH002();
            ZAH002DS.M_USERDataTable tb = instance.GetUser(this.txt用户ID.Text, this.txt旧密码.Text);
            //密码不对
            if (tb.Count < 1)
            {
                this.SetMessage("ZAH002", "EZAH002003", MSG_LEVEL.Error);
                this.txt旧密码.Focus();
                this.AddCssClass(this.txt旧密码, CSS_TEXT_ERROR);
                return;
            }
            isChanged = instance.ChangeUserPassword(this.txt用户ID.Text, this.txt新密码.Text);

            if (!isChanged)
            {
                this.SetMessage("ZAH001", "EZAH001001", MSG_LEVEL.Error);
                this.AddCssClass(this.txt用户ID, CSS_TEXT_ERROR);
                this.AddCssClass(this.txt旧密码, CSS_TEXT_ERROR);
                this.txt用户ID.Focus();
            }
            else
            {
                this.SetMessage("GTH001", "IGTH001001", MSG_LEVEL.Info);

            }

        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="strUserId"></param>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    private Boolean GetUser(String strUserId)
    {
        this.RemoveCssClass(this.txt用户ID, CSS_TEXT_ERROR);
        
        CZAH002 instance = new CZAH002();
        ZAH002DS.M_USERDataTable tb = instance.GetUser(strUserId);

        //
        if (tb.Count > 0)
        {
            this.txt用户名汉字.Text = tb[0].USERID_HANZI.ToString();
            this.txt用户ID.Enabled = false;
        }
        else
        {
            this.SetMessage("ZAH002", "EZAH002002", MSG_LEVEL.Error);
            this.txt用户ID.Enabled = true;
            return false;
        }
        return true;
    }
}